package com.dc.project.controller.user;

import com.dc.project.constant.ControllerConstant;
import com.dc.project.entity.dto.TaskFileCommitDto;
import com.dc.project.entity.vo.TaskFileCommitVo;
import com.dc.project.service.TaskFileCommitService;
import com.own.aop.login.LoginIgnore;
import com.own.aop.operation.LogOperationModuleAop;
import com.own.business.resource.business.service.FileRecordService;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * UserClientTaskFileCommitController
 *
 * @author chenxueli
 * @date 2025-03-10 16:37:53
 */
@Slf4j
@Tag(name = "任务文件提交" + ConstantBeanSuffix.CONTROLLER)
@RestController
@LoginIgnore
@CrossOrigin("*")
@LogOperationModuleAop("任务文件提交")
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + ControllerConstant.PREFIX_USER_API + "task/file/commit")
public class UserClientTaskFileCommitController extends BaseController {

    @Resource
    private TaskFileCommitService taskFileCommitService;
    @Resource
    private FileRecordService fileRecordService;

    @Operation(summary = "添加信息")
    @PostMapping("/")
    public ResultModel<TaskFileCommitVo> add(
            @Validated @RequestBody TaskFileCommitDto dto
    ) {
        dto.check();
        // 查询对应的文件信息
        var fileRecord = fileRecordService.getById(dto.getFileId());
        dto.setFilePath(fileRecord.getPosition() + fileRecord.getPath());
        var updateModel = taskFileCommitService.add(dto.toBo());
        updateModel.getData().setFileRecord(fileRecord);
        return R.success(updateModel);
    }

    @Operation(summary = "删除信息")
    @DeleteMapping("/{commitId}")
    public ResultModel<Boolean> delete(
            @Parameter(description = "主键id", required = true) @PathVariable Long commitId
    ) {
        var updateModel = taskFileCommitService.delete(commitId);
        return R.success(updateModel);
    }

    @Operation(summary = "重新执行解析")
    @PostMapping("/reparse/{commitId}")
    public ResultModel<Boolean> reparse(
            @Parameter(description = "主键id", required = true) @PathVariable Long commitId
    ) {
        taskFileCommitService.reparse(commitId);
        return R.success(true);
    }

}
